package com.pronhub.luowowo.website.web.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.pronhub.luowowo.article.domain.Destination;
import com.pronhub.luowowo.article.domain.StrategyCommend;
import com.pronhub.luowowo.article.domain.StrategyDetail;
import com.pronhub.luowowo.article.domain.StrategyTag;
import com.pronhub.luowowo.article.query.StrategyDetailQuery;
import com.pronhub.luowowo.article.service.*;
import com.pronhub.luowowo.comment.domain.StrategyComment;
import com.pronhub.luowowo.comment.query.StrategyCommentQuery;
import com.pronhub.luowowo.comment.service.IStrategyCommentService;
import com.pronhub.luowowo.member.domain.UserInfo;
import com.pronhub.luowowo.search.query.SearchQueryObject;
import com.pronhub.luowowo.search.service.IStrategySearcheService;
import com.pronhub.luowowo.search.vo.StatisVO;
import com.pronhub.luowowo.service.IStrategyStatisRedisService;
import com.pronhub.luowowo.util.AjaxResult;
import com.pronhub.luowowo.util.Rediskey;
import com.pronhub.luowowo.vo.StrategyStatisVO;
import com.pronhub.luowowo.website.web.annotation.UserToken;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("strategy")
public class StrategyController {


    @Reference
    private IRegionService regionService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyCatalogService strategyCatalogService;
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IStrategyTagService strategyTagService;
    //Reids缓存的所有操作
    @Reference
    private IStrategyStatisRedisService strategyStatisRedisService;
    //文章内容的
    @Reference
    private IStrategyCommentService strategyCommentService;
    //攻略推荐的
    @Reference
    private IStrategyCommendService strategyCommendService;
    //主题前十 用ES的
    @Reference
    private IStrategySearcheService strategySearcheService;
    //一进来的页面
    @RequestMapping("")
    public Object index(Model model){


        //commends 攻略推荐 那个滚动的 前五的攻略图体检
        List<StrategyCommend> commends = strategyCommendService.getTop5();
        model.addAttribute("commends",commends);

        //hotCds 是否热门
        List<StrategyStatisVO> hotCds = strategyStatisRedisService.getHotTop10();
        model.addAttribute("hotCds",hotCds);

        //国内外攻略的排行 查出来全部然后在进行塞
        List<StrategyStatisVO> allVOByZset = strategyStatisRedisService.getAllVOByZset();
        //判断国内外的
        ArrayList<Object> unabroadCds = new ArrayList<>();
        ArrayList<Object> abroadCds = new ArrayList<>();
        for (StrategyStatisVO vo : allVOByZset) {
            if (vo.isIsabroad()){
                if (abroadCds.size()<10){
                    abroadCds.add(vo);
                }
            }else {
                if (unabroadCds.size()<10){
                    unabroadCds.add(vo);
                }
            }
        }
        model.addAttribute("abroadCds",abroadCds);
        model.addAttribute("unabroadCds",unabroadCds);


        //themeCds 主题推荐
        List<Map<String, Object>> themeCds =strategySearcheService.queryThemeTop10();
        model.addAttribute("themeCds",themeCds);


        //全部旅游攻略
        //chinas
        List<StatisVO>  chinas = strategySearcheService.queryConditionProvincesGroup();
        model.addAttribute("chinas",chinas);
        //abroads
        List<StatisVO> abroads = strategySearcheService.queryConditionAbroadsGroup();
        model.addAttribute("abroads",abroads);
        //主题 themes 有数量的那种
        List<StatisVO> themes = strategySearcheService.queryConditionThemeGroup();
        model.addAttribute("themes",themes);
        return "strategy/index";
    }
    @RequestMapping("searchPage")
    public String searchPage(Model model,@ModelAttribute("qo") SearchQueryObject qo){
        Page page = strategySearcheService.query(qo);
        model.addAttribute("page",page);
        return "strategy/searchPageTpl";
    }


    @RequestMapping("detail")
    public Object detail(Model model ,Long id){
        //根据文章的id查出来有什么文章
        StrategyDetail strategyDetail = strategyDetailService.get(id);
        strategyDetail.setStrategyContent(strategyDetailService.getContentById(id));
        model.addAttribute("detail",strategyDetail);

        //阅读数+1
        strategyStatisRedisService.viewNumIncrease(id,1);
        //热门排行分数 + 1  : 浏览数+评论数
        strategyStatisRedisService.addHotScores(Rediskey.STRATEGY_STATIS_VO.getKey(id.toString()),1);

        //页面数据
        model.addAttribute("vo", strategyStatisRedisService.getStrategyStatisVo(id));



        return "strategy/detail";
    }
    @RequestMapping("list")
    public Object detail(Model model , @ModelAttribute("qo")StrategyDetailQuery qo){
        //吐司操作
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts",toasts);
        model.addAttribute("dest",dest);

        //tags
        model.addAttribute("tags",strategyTagService.list());
        //分页
        model.addAttribute("pageInfo",strategyDetailService.query(qo));

        return "strategy/list";
    }
    //commentAdd
    @RequestMapping("/commentAdd")
    @ResponseBody
    public Object commentAdd(StrategyComment comment, @UserToken UserInfo userInfo){

        /*if (userInfo==null){
            try {
                response.sendRedirect("/login.html");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
*/
        comment.setCreateTime(new Date());
        comment.setUserId(userInfo.getId());
        comment.setUsername(userInfo.getNickname());
        comment.setLevel(userInfo.getLevel());
        comment.setHeadUrl(userInfo.getHeadImgUrl());
        comment.setCity(userInfo.getCity());

        strategyCommentService.saveOrUpdate(comment);

        //评论完了更新当前Sid为Key的Redis数据
        //评论数+1
        strategyStatisRedisService.replyNumIncrease(comment.getDetailId(),1);


        return AjaxResult.SUCCESS.addData(strategyStatisRedisService.
                getStrategyStatisVo(comment.getDetailId()).getReplynum());
    }

    //根据文章攻略的id去查出来当前的评论有啥
    @RequestMapping("/comment")
    public String commentList(Model model, @ModelAttribute("qo")StrategyCommentQuery qo){
        Page page =strategyCommentService.selectComment(qo);
        model.addAttribute("page",page);
        return "strategy/commentTpl";
    }
    //攻略点赞
    @RequestMapping("commentThumbUp")
    @ResponseBody
    public Object commentThumbUp(String toid,Long fromid){
        //根据文章的MongoDBId去查出这个文章有什么 然后再进行点赞是否的操作
        strategyCommentService.commentThumbUp(toid,fromid);
        return AjaxResult.SUCCESS;
    }

    //攻略的点赞分享收藏
    @RequestMapping("favor")
    @ResponseBody
    public Object favor(Long sid,@UserToken UserInfo userInfo){
        //1如果内登录
        if (userInfo.getId()==null){
            AjaxResult result = new AjaxResult(false, "需要登录登录");
            result.addData(102);
            return result;
        }

        //收藏还是取消收藏 这些数据放在Redis里面
        boolean ret = strategyStatisRedisService.favor(sid, userInfo.getId());

        //国内外攻略收藏加分减分的
        strategyStatisRedisService.addCommendScores(Rediskey.STRATEGY_STATIS_VO.getKey(sid.toString()),ret?1:-1);

        StrategyStatisVO vo = strategyStatisRedisService.getStrategyStatisVo(sid);
        return AjaxResult.SUCCESS.addData(vo);
    }
    //攻略的点赞的
    @RequestMapping("strategyThumbup")
    @ResponseBody
    public Object strategyThumbup(Long sid,@UserToken UserInfo userInfo) {
        //1如果内登录
        if (userInfo == null ) {
            AjaxResult result = new AjaxResult(false, "需要登录登录");
            result.setCode( 102);
            return result;
        }
        //返回是true 就是点赞成功 加分数 否则减分
        boolean result = strategyStatisRedisService.thumbup(sid, userInfo.getId());
        //点赞那个
        if (result){
            strategyStatisRedisService.addCommendScores(Rediskey.STRATEGY_STATIS_VO.getKey(sid.toString()),1);
        }

        int thumbsupnum = strategyStatisRedisService.getStrategyStatisVo(sid).getThumbsupnum();
        return new AjaxResult(result,null).addData(thumbsupnum);
    }
}
